// pages/driver/addIdCardPhoto/addIdCardPhoto.js
const handler = require("../../../utils/function.js");
var app = getApp(),
    api = require("../../../api.js");
Page({
    /**
     * 页面的初始数据
     */
    data: {
        pageType: null,
        fontImage: null,
        backImage: null,
        idCardPhoto: null,
        idCardPhotoTest: false,
        driverPhoto: null,
        driverPhotoTest: false,
        positive: '', //驾照正面照片
        reverse: '', //驾照反面照片
        idreverse: '', //身份证反面照片
        idpositive: '', //身份证正面照片
        drivers: "",
        isChange:false
    },
    // 图片正面上传
    upFontloadFile: function(e) {
      if (this.data.isChange){
        return
      }
        var type = e.currentTarget.dataset.type;
        const that = this;
        wx.showLoading()
        wx.chooseImage({
            success: function(res) {
                var tempFilePaths = res.tempFilePaths
                wx.uploadFile({
                    url: api.driver.uploadcar, //仅为示例，非真实的接口地址
                    filePath: tempFilePaths[0],
                    name: 'file',
                    formData: {
                        'access_token': wx.getStorageSync("access_token"),
                        '_uniacid': app.siteInfo.uniacid,
                        '_acid': app.siteInfo.acid,
                    },
                    success: function(res) {
                      wx.hideLoading()
                      var jon = JSON.parse(res.data);
                      if (jon.code == 0) {
                        wx.showToast({
                          title: jon.msg,
                          icon: 'none'
                        })
                        return
                      }
                    if (jon.code == 1) {
                        that.setData({
                          idpositive: jon.data
                        })
                        wx.setStorageSync("idpositive", jon.data)
                    }
                    }
                })
            }
        })
    },
    // 图片反面上传
    upBackloadFile: function(e) {
 
 
      if (this.data.isChange) {
        return
      }
        const that = this;
        var type = e.currentTarget.dataset.type;
        wx.chooseImage({
            success: function(res) {
                var tempFilePaths = res.tempFilePaths
              wx.showLoading()
                wx.uploadFile({
                    url: api.driver.uploadcar, //仅为示例，非真实的接口地址
                    filePath: tempFilePaths[0],
                    name: 'file',
                    formData: {
                        'user': 'test',
                        'access_token': wx.getStorageSync("access_token"),
                        '_uniacid': app.siteInfo.uniacid,
                        '_acid': app.siteInfo.acid,
                    },
                    success: function(res) {
                      wx.hideLoading()
                        if (res.statusCode == 200) {
                           
                            var jon = JSON.parse(res.data);
                          if (jon.code == 0) {
                            wx.showToast({
                              title: jon.msg,
                              icon: 'none'
                            })
                            return
                          }
                            if (jon.code == 1) {
                                that.setData({
                                    idreverse: jon.data
                                })
                              wx.setStorageSync("idreverse", jon.data)
                            }
                        } else {
                        }
                    }
                })
            }
        })
    },
    // intoPageHandler 页面跳转
    intoPageHandler: function(e) {
      let that=this;
     
      if (this.data.idpositive && this.data.idreverse ){
        wx.setStorageSync("idpositive", this.data.idpositive)
        wx.setStorageSync("idreverse", this.data.idreverse)
        wx.showToast({
          title: '上传成功',
          success:function(){
            setTimeout(()=>{
              wx.navigateBack({
                delta: 1
              })
            },1500)
            
          }
        })
       
      } 
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
      // 获取优惠券
      if (options.uid) {
        app.shareCouponsMessage(options.uid)
      }
      // 判断是否可以修改图片
      if (options.isChange == 'true'){
        this.setData({ isChange: true})
      }
      
      
        /*        wx.setStorageSync("driverss",options.driver)*/
        if ( options.type == 'driverPhoto') {
            if (wx.getStorageSync('driving_license') != '') {
                this.setData({
                    positive: wx.getStorageSync('driving_license').driverfPhoto,
                    reverse: wx.getStorageSync('driving_license').driverPhoto
                })
            }
        }
        if ( options.type == 'idCardPhoto') {
          if(wx.getStorageSync('id_card_pic')!=''){
             this.setData({
                positive: wx.getStorageSync('id_card_pic').id_card_pic,
                reverse: wx.getStorageSync('id_card_pic').id_card_fpic
            })
          }

          console.log('正面正面',this.data.reverse)
          console.log('正面正面22',this.data.pageType)
           
        }
        this.setData({
            pageType: options.type,
        });
      
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
      var idpositive = wx.getStorageSync("idpositive")||'';
      var idreverse = wx.getStorageSync("idreverse") || '';
      console.log(idpositive,'------')
        this.setData({
          driver: wx.getStorageSync("driver_information"), idpositive, idreverse,
        });
    },
  // 用户分享
  onShareAppMessage: function () {
    let title = app.shareAppMessage().title;
    let imageUrl = app.shareAppMessage().imageUrl;
    let desc = app.shareAppMessage().desc;
    let path = app.shareAppMessage().path;
    console.log(app.shareAppMessage())
    return {
      title: title,
      imageUrl: imageUrl,
      desc: desc,
      path: path,
    }
  },
})